<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * 用户炫酷值
 * Class UserCoolval
 * @package App\Http\Model
 */
class UserCoolval extends Base
{
    protected $table = 'user_coolval';
    protected $tableDetails = 'user_coolval_details';
    protected $tableLevel = 'user_coolval_level';

    protected $tablePrefix = 'zb_';

    protected $CacheCoolvalLevelKey = 'CACHA:COOLVAL:LEVEL';




    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);

        $this->tablePrefix = env('DB_PREFIX');
    }


    /************************* $table ***********************/


    public function addUserCoolval($data)
    {
        return DB::table($this->table)->insert($data);
    }


    /**
     * 增加或减少给定列（炫酷）的值
     * @param $UID
     * @param int $change_coolval
     * @param int $cool_level
     * @return int
     */
    public function incUserCoolval($UID, $change_coolval=1, $cool_level=0)
    {
        $obj = DB::table($this->table)
                ->where('user_id', $UID);

        if ($cool_level){
            $res = $obj->increment('cool_val', $change_coolval, ['cool_level' => $cool_level,'edit_time'=>time()]);
        }else{
            $res = $obj->increment('cool_val', $change_coolval, ['edit_time'=>time()]);
        }
        return $res;
    }


    /**
     * 获取用户炫酷值
     * @param $userId
     * @return int
     */
    public function getUserCoolval($userId):int
    {
        $coolval = DB::table($this->table)->where('user_id', $userId)->value('cool_val');

        return (int) $coolval;
    }

    public function getUserCoolInfo($userId)
    {
        $info = DB::table($this->table)->where('user_id', $userId)->first();
        return $this->objToArray($info);
    }





    /************************* $tableDetails ***********************/

    function addUserCoolDetails(array $data)
    {
        return DB::table($this->tableDetails)->insert($data);
    }

    /**
     * sku炫酷值是否累计
     * @param $UID
     * @param $GID
     * @return bool
     */
    public function ifUserGoodsCool($UID, $GID):bool
    {
        return DB::table($this->tableDetails)
                    ->where('user_id', $UID)
                    ->where('goods_id', $GID)
                    ->exists();
    }






    /************************* $tableLevel ***********************/
    public function getCoolLevel()
    {
        $CacheKey = $this -> CacheCoolvalLevelKey;
        $Cache = Redis::get($CacheKey);
        $res = json_decode($Cache, true);
        if(empty($res)){
            $res = DB::table($this->tableLevel)->get();
            $res = self::objToArray($res);
            if ($res){
                Redis::set($CacheKey, json_encode($res), 'EX', 7200);
            }
        }
        return $res;
    }

    public function getCoolLevelInfo($where)
    {
        $info = DB::table($this->tableLevel)->where($where)->first();
        return self::objToArray($info);
    }

    public function getLevelByVal($val)
    {
        $sql = 'select * from ' . $this->tablePrefix . $this->tableLevel . ' where val<=' . $val . ' and (val_max>=' . $val . ' or val_max=0) limit 1';
        $bindings = array($val, $val);
        $re = DB::select($sql, $bindings);
        return self::objToArray($re[0]);
    }


}

